package com.jiangnan.warehouse_management.controller;

import com.jiangnan.warehouse_management.common.Result;
import com.jiangnan.warehouse_management.dto.request.ItemRequest;
import com.jiangnan.warehouse_management.dto.request.ItemUpdateRequest;
import com.jiangnan.warehouse_management.dto.response.ItemDetailResponse;
import com.jiangnan.warehouse_management.dto.response.ItemQueryResponse;
import com.jiangnan.warehouse_management.pojo.Item;
import com.jiangnan.warehouse_management.service.ItemService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class ItemController {

    private final ItemService itemService;

    @GetMapping("/getItems")
    public Result<ItemQueryResponse> getItems(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "itemName", required = false) String itemName,
            @RequestParam(value = "categoryId", required = false) Integer categoryId) {
        ItemQueryResponse data = itemService.getItems(page, pageSize, itemName, categoryId);
        return Result.success(data);
    }

    @PostMapping("/createItem")
    public Result<Item> createItem(@Valid @RequestBody ItemRequest request) {
        Item data = itemService.createItem(request);
        return Result.success(data);
    }

    @GetMapping("/getItem/{id}")
    public Result<ItemDetailResponse> getItemById(@PathVariable Integer id) {
        ItemDetailResponse data = itemService.getItemById(id);
        return Result.success(data);
    }

    @PutMapping("/updateItem/{id}")
    public Result<ItemDetailResponse> updateItem(
            @PathVariable Integer id,
            @Valid @RequestBody ItemUpdateRequest request) {
        ItemDetailResponse data = itemService.updateItem(id, request);
        return Result.success(data);
    }

    @DeleteMapping("/deleteItem/{id}")
    public Result<Void> deleteItem(@PathVariable Integer id) {
        itemService.deleteItem(id);
        return Result.success();
    }
}
